"""
Case Type   : resource pool
Case Name   : 验证普通资源池mempercent默认值0，无相加<=100%限制
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建普通资源池关联workload_a1控制组,不指定mem_percent,查询
    3.创建普通资源池关联workload_a1控制组,两个资源池mem_percent之和大于100,查询
    4.环境清理
Expect      :
    1.创建成功
    2.创建成功，默认mem_percent=0
    3.创建成功
    4.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_name = 'class_a'
        self.group_name = 'workload_a1'
        self.respool = ('resp_resource_pool_0047_1',
                        'resp_resource_pool_0047_2',
                        'resp_resource_pool_0047_3')

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_name} -s 20;" \
              f"gs_cgroup -c -S {self.class_name} -G {self.group_name} -g 20" \
              f"; gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_name}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_name}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建普通资源池关联workload_a1控制组,不指定" \
               "mem_percent,查询;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[0]} with(control_group = "
            f"'{self.class_name}:{self.group_name}'); select pg_sleep(3);"
            f"select respool_name, mem_percent from pg_resource_pool "
            f"where respool_name = '{self.respool[0]}';")
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')
        self.assertIn(f'{self.respool[0]} |           0', sql_res,
                      f'执行失败:{text}')

        text = "-----step3:创建普通资源池关联workload_a1控制组，两个资源池" \
               "mem_percent之和大于100，查询;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[1]} with(control_group = "
            f"'{self.class_name}:{self.group_name}', mem_percent = 80);"
            f"create resource pool {self.respool[2]} with(control_group = "
            f"'{self.class_name}:{self.group_name}', mem_percent = 25);"
            f"select pg_sleep(3);"
            f"select respool_name, mem_percent from pg_resource_pool "
            f"where respool_name in('{self.respool[1]}','{self.respool[2]}');")
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')
        self.assertIn(f'{self.respool[1]} |          80\n'
                      f' {self.respool[2]} |          25\n'
                      f'(2 rows)', sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step4:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            ';\n'.join(
                [f"drop resource pool if exists {i}" for i in self.respool]))
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertEqual(3, drop_res.count(
            self.constant.drop_resource_pool_success), f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
